/**
 * Created by Limit on 2017/8/31.
 */


var APP_NOT_SUBMITTED="/cykmr/app/img/not-submit.png";
var APP_SUBMITTED_SUCCESS="/cykmr/app/img/submit-success.png";
var APP_SUBMITTED_FAILED="/cykmr/app/img/submit-failed.png";

function initializeAppStatus(jobId){
    queryAppStatus(jobId,bindAppSubmitStatus);
    $("#app-update-btn").click(function () {
        queryAppStatus(jobId,bindAppSubmitStatus);
    });


}

function queryAppStatus(jobId,callback) {
    var url="/cykmr/manage/getJobStatus";
    $.ajax({
       url:url,
        dataType:'json',
        data:{
           jobId:jobId
        },
        success:function(data) {
            callback(jobId,data);
        },
        error:function () {
            callback(jobId,null);
        }
    });
}

function bindAppSubmitStatus(jobId,data) {

    var $appSubmitBtn=$("#app-submit-btn");

    var $appKillBtn=$("#app-kill-btn");

    if(data===null){
        setSubmitStatus("未提交");
        setProcessStatus("未运行");
        $appSubmitBtn.removeAttr("disabled").click(function () {
           submitApp(jobId);
        });
    }else if(data.success){
        setSubmitStatus("提交成功");
        if(data.driverState==="ERROR"){
            setProcessStatus("运行错误");
            $appSubmitBtn.removeAttr("disabled").css("cursor","");
        }else if(data.driverState==="RUNNING"){
            setProcessStatus("正在运行");
            $appSubmitBtn.addClass("tip");
            $appSubmitBtn.css("cursor","not-allowed");
            $appSubmitBtn.removeAttr('onclick');
        }
        else if(data.driverState==="FINISHED"){
            setProcessStatus("运行完成");
            $appSubmitBtn.addClass("tip");
            $appSubmitBtn.css("cursor","not-allowed");
            $appSubmitBtn.removeAttr('onclick');
        }else if(data.driverState==="FAILED"){
            setProcessStatus("运行失败");
            $appSubmitBtn.removeAttr("disabled").css("cursor","");
        }else {

        }
    }else {
        setSubmitStatus("未提交");
        setProcessStatus("未运行");
        $appSubmitBtn.removeAttr("disabled").click(function () {
            submitApp(jobId);
        });
    }
    $appKillBtn.addClass("tip");
    $appKillBtn.css("cursor","not-allowed");
    $appKillBtn.removeAttr('onclick');
    $(".tip").mouseover(function(){
        var _this = $(this);
        _this.justToolsTip({
            animation:"flipIn",
            contents:"已禁止，不可用",
            gravity:'bottom'
        });
    });
}

function submitApp(jobId) {
    var url="/cykmr/manage/startJob";
    $.ajax({
       url:url,
        dataType:'json',
        data:{
            jobId:jobId
        },
        success:function (data) {
            console.log(data);
            var $appSubmitBtn=$("#app-submit-btn");


            if(data.submit==1){
                setSubmitStatus("提交成功");
            }else{
                setSubmitStatus("提交失败");
            }
            if(data.success==1){
                setProcessStatus("运行完成");
                $appSubmitBtn.addClass("tip");
                $appSubmitBtn.css("cursor","not-allowed");
                $appSubmitBtn.removeAttr('onclick');
            } else{
                setSubmitStatus("未提交");
            }
            if(data.error==1){
                setProcessStatus("运行失败");
                $appSubmitBtn.removeAttr("disabled").css("cursor","");
            }
        },
        error:function () {
            alert("提交请求失败！");
        }
    });
}

function stopApp(jobId) {
    var url="/cykmr/manage/stopJob";
    $.ajax({
        url:url,
        dataType:'json',
        data:{
            jobId:jobId
        },
        success:function (data) {
            console.log(data);
            if(data.success){
                setProcessStatus("未运行");
            }else {
                alert("APP终止失败！");
            }
        },
        error:function () {
            alert("APP终止失败！");
        }
    });
}

function setSubmitStatus(status) {
    $("#app-submit-status").val(status);
}
function setProcessStatus(status) {
    $("#app-process-status").val(status);
}

